 import { createStore } from 'vuex'
 import { getSelfRoleNode } from '@/api/router';
 
 let url_route = [] // 菜单路由
 let node_route = [] // 节点路由
 
export default createStore({
  state: {
    menu:"",
	node:""
  },
  getters: {
 
  },
  mutations: {
    setrole(state,datas){
      state.menu = datas.menu
      state.node = datas.nodes
    }
  },
  actions: {
    async getrole({commit}){
			let menu = []
			let nodes = []
			if(url_route.length == 0 || node_route.length == 0){
				await getSelfRoleNode().then(res=>{
					if(res.state == 200){
						for(let item of res.data){
							// 获取 状态开启并且类型为菜单
							if(item.status == 1){
								menu.push(item)
								if(item.is_button == 1){ // 节点为按钮的数据
									nodes.push(item)
								}
							}
						}

						url_route = menu
						node_route = nodes
					}
				})
			}else{
				menu = url_route
				nodes = node_route
			}
			
			let datas = {
				menu:menu, // 菜单路由
				nodes:nodes // 节点接口
			}
      commit("setrole", datas)  //将权限数组通过mutations修改state里的role数据
    },
		// 清除权限
		cleanup(){
			url_route = []
			node_route = []
			console.log('清除权限')
		}
  },
  modules: {
  }
})